package com.maixy.esdemo.test;

import com.alibaba.fastjson.JSON;
import org.apache.http.HttpHost;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import java.io.IOException;

/**
 * @ClassName ESTestDocInsert
 * @Description TODO
 * @Date 2021/4/16 22:40
 * @Author maixy
 */
public class ESTestDocInsert {
    public static void main(String[] args) throws IOException {
        RestHighLevelClient esClient =new RestHighLevelClient(
                RestClient.builder(new HttpHost("localhost",9200, "http")));

        //插入数据
        IndexRequest request = new IndexRequest();
        request.index("user").id("1001");

        User user = new User("张三", "男", 20);
        String userJson = JSON.toJSONString(user);
        request.source(userJson, XContentType.JSON);

        IndexResponse response = esClient.index(request, RequestOptions.DEFAULT);
        System.out.println(response.getResult());

        esClient.close();
    }
}
